﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SdmxMl.Common;

namespace SdmxMl.SmsImportCfg
{
    public class WorkSheetHelper
    {
        private DataTable _dtWorksheet;

        public WorkSheetHelper(DataTable dt)
        {
            _dtWorksheet = dt;
        }

        public int ColCount
        {
            get
            {
                int count = 0;
                if (_dtWorksheet != null)
                    count = _dtWorksheet.Columns.Count;

                return count;
            }
        }

        public int RowCount
        {
            get
            {
                int count = 0;
                if (_dtWorksheet != null)
                    count = _dtWorksheet.Rows.Count;

                return count;
            }
        }

        /// <summary>
        /// Retrieve content of worksheet cell
        /// </summary>
        /// <param name="row">row index (from 1)</param>
        /// <param name="col">col index (from 1)</param>
        /// <returns>string cell content or empty string if nothing found</returns>
        public string GetCellContent(int row, int col)
        {
            return GetCellContent(row, col, string.Empty);
        }

        public string GetVersion(int row, int col, string dflt)
        {
            string vers = GetCellContent(row, col, dflt);
            if (vers.Length == 1)
                vers += ".0";
            return vers;
        }

        public string GetDateStr(int row, int col)
        {
            string val = GetCellContent(row, col, string.Empty);
            if (val.Length > 0)
            {
                try
                {
                    string d = DateTime.FromOADate(Convert.ToDouble(val)).ToString("yyyy-MM-dd");
                    val = d;
                }
                catch { }
            }
            return val;
        }

        /// <summary>
        /// Retrieve content of worksheet cell
        /// </summary>
        /// <param name="row">row index (from 1)</param>
        /// <param name="col">col index (from 1)</param>
        /// <param name="dflt">default text if nothing found</param>
        /// <returns>string cell content or default if nothing found</returns>
        public string GetCellContent(int row, int col, string dflt)
        {
            string content = dflt;
            if (row > 0 && col > 0)
            {
                // C# Indexes from zero, so reduce specified one
                --row;
                --col;

                if (row < _dtWorksheet.Rows.Count && col < _dtWorksheet.Columns.Count)
                {
                    content = _dtWorksheet.Rows[row][col].ToString();
                    if (content == string.Empty)
                        content = dflt;
                }
                else
                    throw new ApplicationException(string.Format("Invalid cell location request {0}:{1}", row, col));
            }

            return content.Trim();
        }

        /// <summary>
        /// Construct a Multilang text according to supported languages and to columns indexes 
        /// indicated in mapping.
        /// </summary>
        /// <param name="row">Actual row index (from 1) of the Worksheet</param>
        /// <param name="cols">column indexes list (semi column separated)</param>
        /// <returns>Multilang Object containing all defined texts</returns>
        public MultiLang GetMultilang(int row, int[] cols)
        {
            MultiLang ml = new MultiLang();
            GetMultilang(ml, row, cols);
            return ml;
        }

        public void GetMultilang(MultiLang ml, int row, int[] cols)
        {
            ml.Clear();
            if (cols != null)
            {
                int lg = 0;
                foreach (string lang in LangSupport.SupportedLanguages)
                {
                    if (lg < cols.Length)
                    {
                        if (cols[lg] != 0)
                            ml.TextList.Add(new TextType(lang, GetCellContent(row, cols[lg])));
                    }
                    ++lg;
                }
            }
        }
     
    }
}
